package com.base.helper;

import com.base.comm.CommUrls;
import com.base.po.param.VosModelList;
import com.base.strategy.factory.BooleanToIntFactory;
import com.base.utils.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 链接VOS系统 的帮助类
 * 
 * @author Administrator
 *
 */
public class VosHelper {

	static Logger logger = LoggerFactory.getLogger(VosHelper.class);
	public String bh(String caller, String callee164, String token, String id) {
		String datas = "{\"Callid\":\"" + id
				+ "\",\"App_id\":\"20151\",\"Appkey\":\"B6dfOt3GaNuouyt8mBrZ1BTQqoIQl02A\",\"Caller\":\"" + caller
				+ "\",\"Callee\":\"" + callee164 + "\",\"Cdr_url\":\"http://admin.xsroem.com/Vm9pcEFwaQ/OSDIHF\"}";
		String callresult = WebHelper.PostData("http://118.31.238.194:8088/api/CallRequest", datas,
				"application/json", token);
		return callresult;
		// string datas = "{\"Caller\":\"" + caller + "\",\"Callee\":\"" +
		// callee164 + "\",\"CustParam\":\"" + id +
		// "\",\"DevID\":\"SuiBianDa\",\"ReturnUrl\":\"http://admin.xsroem.com/Vm9pcEFwaQ/OSDIHF\"}";
		// string callresult =
		// WebHelper.PostData("http://api.shvoip.com:8088/api/callback", datas,
		// "application/json", Encoding.UTF8, token, false);
		// CallInfo2 ca = JsonHelper.JsonToClass<CallInfo2>(callresult);
		// return ca;
	}

	/**
	 * 官方接口
	 */
	// 查询账户
	public static String GetVosAccount(String data) {
		String vosJson = null;

		switch (BooleanToIntFactory.typeToInt(data.contains("1514355907"))){
			case 0:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "GetCustomer", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				break;
			case 1:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "GetCustomer", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				VosModelList v1 = GsonUtil.getObject(vosJson, VosModelList.class);
				if (v1.retCode == 0 && v1.infoCustomers.length < 1){
					data = data.replace("1514355907", "1546837580");
					vosJson = WebHelper.doPost(CommUrls.BaseUrl + "GetCustomer", data,
							"application/x-www-form-urlencoded;charset=utf-8");
				}
				break;
		}

		return vosJson;
	}

	// 添加账户
	public static String CreateVosAccount(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "CreateCustomer", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 修改账户
	public static String ModifyVosAccount(String data) {
		String vosJson = null;

		switch (BooleanToIntFactory.typeToInt(data.contains("1514355907"))){
			case 0:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "ModifyCustomer", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				break;
			case 1:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "ModifyCustomer", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				VosModelList v1 = GsonUtil.getObject(vosJson, VosModelList.class);
				if (v1.retCode == -10007){
					data = data.replace("1514355907", "1546837580");
					vosJson = WebHelper.doPost(CommUrls.BaseUrl + "ModifyCustomer", data,
							"application/x-www-form-urlencoded;charset=utf-8");
				}
				break;
		}

		return vosJson;
	}

	// 删除账户
	public static String DeleteVosAccount(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "DeleteCustomer", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 充值
	public static String PayVosAccount(String data) {
		String vosJson = null;

		switch (BooleanToIntFactory.typeToInt(data.contains("1514355907"))){
			case 0:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "Pay", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				break;
			case 1:
				vosJson = WebHelper.doPost(CommUrls.BaseUrl + "Pay", data,
						"application/x-www-form-urlencoded;charset=utf-8");
				VosModelList v1 = GsonUtil.getObject(vosJson, VosModelList.class);
				if (v1.retCode == -10007){
					data = data.replace("1514355907", "1546837580");
					vosJson = WebHelper.doPost(CommUrls.BaseUrl + "Pay", data,
							"application/x-www-form-urlencoded;charset=utf-8");
				}
				break;
		}

		return vosJson;
	}

	// 创建在用电话卡
	public static String CreateVosActivePhoneCard(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "CreateActivePhoneCard", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 修改在用电话卡
	public static String ModifyVosActivePhoneCard(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "ModifyActivePhoneCard", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 查询在用电话卡
	public static String GetVosActivePhoneCard(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "GetActivePhoneCard", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 删除在用电话卡
	public static String DeleteVosActivePhoneCard(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "DeleteActivePhoneCard", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 创建绑定号码
	public static String CreateVosBindedE164(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "CreateBindedE164", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 话单查询
	public static String GetVosCdr(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "GetCdr", data, "application/x-www-form-urlencoded;charset=utf-8");
	}

	// 创建落地网关
	public static String CreateVosGatewayRouting(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "CreateGatewayRouting", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 修改落地网关
	public static String ModifyVosGatewayRouting(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "ModifyGatewayRouting", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 删除落地网关
	public static String DeleteVosGatewayRouting(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "DeleteGatewayRouting", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 查询落地网关
	public static String GetVosGatewayRouting(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "GetGatewayRouting", data,
				"application/x-www-form-urlencoded;charset=utf-8");
	}

	// 回拨
	public static String VosCallBack(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "CallBack", data, "application/x-www-form-urlencoded;charset=utf-8");
	}

	// 查询历史话单
	public static String VosGetCallInfo(String data) {
		return WebHelper.doPost(CommUrls.BaseUrl + "GetCdr", data, "application/x-www-form-urlencoded;charset=utf-8");
	}

	// 注册
	public static boolean Register(String mobile, String agentname, String validtime, String money, String freegroup) {
		String vosaccount = CreateVosAccount("{\"account\":\"" + mobile + "\",\"name\":\"" + mobile + "\",\"money\":\""
				+ money + "\",\"limitMoney\":\"0\",\"feeRateGroup\":\"" + freegroup
				+ "\",\"type\":\"0\",\"lockType\":\"0\",\"agentAccount\":\"" + agentname
				+ "\",\"phoneBookLimit\":\"0\",\"validTime\":\"" + validtime + "\"}");
		VosModelList vl = GsonUtil.getObject(vosaccount, VosModelList.class);
		System.out.println("注册debug：" + vosaccount);
		if (vl.retCode == 0) {
			String vosphonecard = CreateVosActivePhoneCard(
					"{\"pin\":\"" + mobile + "\",\"account\":\"" + mobile + "\",\"password\":\"123456\"}");
			VosModelList v2 = GsonUtil.getObject(vosphonecard, VosModelList.class);
			if (v2.retCode == 0) {
				String vosbind164 = CreateVosBindedE164("{\"e164\":\"" + mobile + "\",\"pin\":\"" + mobile + "\"}");
				VosModelList v3 = GsonUtil.getObject(vosbind164, VosModelList.class);
				if (v3.retCode == 0) {
					return true;
				} else {
					DeleteVosActivePhoneCard("{\"pin\":\"" + mobile + "\"}");
					DeleteVosAccount("{\"account\":\"" + mobile + "\"}");
					return false;
				}
			} else {
				DeleteVosAccount("{\"account\":\"" + mobile + "\"}");
				return false;
			}

		} else
		{
			logger.warn("VOS retCode:{}",vl.retCode);
			return vl.retCode == -10004;}
	}

	// #region[查询历史话单]
	// public static InfoCdr[] GetHistoryInfo(String caller, String callee,
	// String begintime, String endtime,
	// String type) {
	// String data = "";
	// if (type == "0") {
	// data += "{\"accounts\":[\"IVR\"]";
	// } else {
	// data += "{\"accounts\":[\"" + caller + "\"]";
	// }
	// if (caller != "" && caller != null) {
	// data += ",\"callerE164\":\"" + caller + "\"";
	// }
	// if (callee != "" && callee != null) {
	// data += ",\"calleeE164\":\"" + callee + "\"";
	// }
	// if (begintime != null && begintime != "") {
	// data += ",\"beginTime\":\"" + begintime + "\"";
	// } else {
	// data += ",\"beginTime\":\"" + DateTime.Now.ToString("yyyyMMdd") + "\"";
	// }
	// if (endtime != null && endtime != "") {
	// data += ",\"endTime\":\"" + endtime + "\"";
	// } else {
	// data += ",\"endTime\":\"" + DateTime.Now.AddDays(1).ToString("yyyyMMdd")
	// + "\"";
	// }
	// data += "}";
	// String result = VosGetCallInfo(data);
	// VosModelList v1 = JsonHelper.JsonToClass < VosModelList > (result);
	// return v1.infoCdrs;
	// }

}
